package com.social.instadownloader.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Locale;

import android.os.Environment;
import android.util.Log;

import com.social.instadownloader.model.InstaFileModel;
import com.social.instadownloader.model.InstaFileModel.InstaFileType;

public class InstaFileManager {

	public static final String INSTA_DOWNLOADER_FOLDER = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+CommonUtil.INSTA_DIRECTORY_NAME+"/";
	
	public static ArrayList<InstaFileModel> getListOfInstaFiles(){
		File instaPathDir = new File(INSTA_DOWNLOADER_FOLDER);
		File listFiles[] = instaPathDir.listFiles();
		ArrayList<InstaFileModel> listOfMediaFiles = new ArrayList<InstaFileModel>();
		if (listFiles!=null && listFiles.length>0) {
			int fileSize = listFiles.length;
			for (int i = 0; i < fileSize; i++) {
				InstaFileModel model = new InstaFileModel();
				model.name = listFiles[i].getName();
				model.absolutePath = listFiles[i].getAbsolutePath();
				if (IsSupportedImageFile(listFiles[i].getAbsolutePath())) {
					model.fileType = InstaFileType.IMAGE;
					listOfMediaFiles.add(model);
				}else if(IsSupportedVideoFile(listFiles[i].getAbsolutePath())){
					model.fileType = InstaFileType.VIDEO;
					listOfMediaFiles.add(model);
				}
			}
		}
		return listOfMediaFiles;
	}
	/*
	 * Check supported file extensions
	 * 
	 * @returns boolean
	 */
	private static boolean IsSupportedImageFile(String filePath) {
		String ext = filePath.substring((filePath.lastIndexOf(".") + 1),
				filePath.length());

		if (CommonUtil.IMAGE_FILE_EXTENTION.contains(ext
				.toLowerCase(Locale.getDefault())))
			return true;
		else
			return false;

	}
	
	/*
	 * Check supported file extensions
	 * 
	 * @returns boolean
	 */
	private static boolean IsSupportedVideoFile(String filePath) {
		String ext = filePath.substring((filePath.lastIndexOf(".") + 1),
				filePath.length());

		if (CommonUtil.VIDEO_FILE_EXTENTION.contains(ext
				.toLowerCase(Locale.getDefault())))
			return true;
		else
			return false;

	}
	
}
